import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入login组件
import Login from '../components/Login.vue'
// 导入home组件
import Home from '../components/Home.vue'
// 导入后台首页的子组件welcome
import Welcome from '../components/Welcome.vue'
// 导入用户列表组件
import Users from '../components/user/Users.vue'
// 导入权限管理组件
import Rights from '../components/power/Rights.vue'
// 角色列表组件
import Roles from '../components/power/Roles.vue'
// 商品列表组件
import Cate from '../components/goods/Cate.vue'
// 分类参数组件
import Params from '../components/goods/Params.vue'
// 商品列表组件
import GoodsList from '../components/goods/GoodsList.vue';
// 商品列表添加页
import GoodsAdd from '../components/goods/GoodsAdd.vue';
// 商品编辑页面
import GoodsEdit from '../components/goods/GoodsEdit.vue';
// 订单列表组件
import Order from '../components/order/Order.vue';
// 数据统计组件
import Reports from '../components/reports/Reports.vue';
Vue.use(VueRouter)
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    // home     默认重定向到welcome
    redirect: '/welcome',
    children: [
      // 因为welcome组件属于home的子组件，所以渲染的时候home先渲染
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      // 权限管理组件
      { path: '/rights', component: Rights },
      // 角色列表路由
      { path: '/roles', component: Roles },
      // 商品分类路由
      { path: '/categories', component: Cate },
      // 分类参数路由
      { path: '/params', component: Params },
      // 商品列表路由
      { path: '/goods', component: GoodsList },
      // 商品添加路由
      { path: '/add', component: GoodsAdd },
      // 商品商品编辑路由
      { path: '/edit', component: GoodsEdit },
      // 订单列表路由
      { path: '/orders', component: Order },
      // 数据统计路由
      { path: '/reports', component: Reports }
    ]
  }
]
const router = new VueRouter({
  routes
})
// 挂载路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    // 没有token表示未登录强制跳转到login页面去登录
    return next('/login')
  }
  next()
})
export default router
